package net.kingborn.erp.fc.command;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.kingborn.core.command.BaseCommand;
import net.kingborn.core.command.Command;
import net.kingborn.core.command.Param;
import net.kingborn.core.exception.Assert;
import net.kingborn.core.exception.BizException;
import net.kingborn.core.util.StrKit;
import net.kingborn.erp.constant.Define;
import net.kingborn.erp.fc.model.Income;
import net.kingborn.erp.fc.model.AccountRecord;
import net.kingborn.erp.fc.model.FlowRecord;
import net.kingborn.erp.fc.service.AccountRecordService;
import net.kingborn.erp.fc.service.FlowRecordService;
import net.kingborn.erp.fc.service.IncomeService;
import net.kingborn.erp.uc.model.Product;
import net.kingborn.erp.uc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 收入单保存
 */
@Command
public class CIncomeSave extends BaseCommand {

    @Autowired
    private IncomeService incomeService;
    @Autowired
    private AccountRecordService accountService;
    @Autowired
    private FlowRecordService recordService;

    @Autowired
    private ProductService productService;

    @Param(required = true)
    private Income income;
    @Param(defaultValue = "[]")
    private List<FlowRecord> recordList;
    @Param(defaultValue = "[]")
    private List<AccountRecord> accountList;

    private Income persistedIncome;

    @Override
    protected void init() throws Exception {

    }

    @Override
    protected void doCommand() throws Exception {
        if (recordList == null || recordList.size() == 0) return;
        incomeService.deleteByIncome(income.getCode());
        persistedIncome = new Income();
        if (StrKit.notBlank(income.getId())) {
            persistedIncome.setId(income.getId());
        }
        persistedIncome.setCode(income.getCode());
        persistedIncome.setIssueDate(income.getIssueDate());
        persistedIncome.setCollectAmount(income.getCollectAmount());
        persistedIncome.setListerId(income.getListerId());
        persistedIncome.setRemark(income.getRemark());
        incomeService.saveOrUpdate(persistedIncome);

        // 新增单据
        addRecordList();

        data.put("income", persistedIncome);
    }

    /**
     * 新增记录列表
     */
    private void addRecordList() {
        if (recordList == null || recordList.size() == 0) return;

        List<FlowRecord> persistedRecordList = new ArrayList<>();
        FlowRecord persistedRecord = new FlowRecord();
        for (FlowRecord record : recordList) {
            persistedRecord = new FlowRecord();
            persistedRecord.setIssueDate(persistedIncome.getIssueDate());
            persistedRecord.setBusinessType(Define.BUSINESS_TYPE_INCOME);
            persistedRecord.setBusinessId(persistedIncome.getId());
            persistedRecord.setCategoryId(record.getCategoryId());
            persistedRecord.setAmount(record.getAmount());
            persistedRecord.setRemark(record.getRemark());
            persistedRecord.setProductId(record.getProductId());
            persistedRecord.setProductCount(record.getProductCount());
            persistedRecord.setCustomerName(record.getCustomerName());
            persistedRecord.setCustomerPhone(record.getCustomerPhone());
            persistedRecord.setPriceType(record.getPriceType());

            persistedRecordList.add(persistedRecord);
            FlowRecord f = recordService.findListById(record.getId());
            Product p1 = productService.getOne(new QueryWrapper<Product>().eq("id", record.getProductId()));

            Product product = new Product();
            product.setId(record.getProductId());
            product.setMaximumStock(p1.getMaximumStock());
            if (null != f) {
                product.setMinimumStock(p1.getMinimumStock() - f.getProductCount());
                productService.updateByProduct(product);
            }
            Product p2 = productService.getOne(new QueryWrapper<Product>().eq("id", record.getProductId()));
            product.setMinimumStock(p2.getMinimumStock() + record.getProductCount());
            productService.updateByProduct(product);


        }
        recordService.deleteByBusiness(persistedRecord.getBusinessId());
        recordService.saveBatch(persistedRecordList);
    }
}
